package com.jintoufs.service.baseinfo.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.jhj.common.redis.JhjRedis;
import com.jintoufs.dao.baseinfo.TickerDAO;
import com.jintoufs.domain.baseinfo.Depth;
import com.jintoufs.domain.baseinfo.DepthUnit;
import com.jintoufs.domain.baseinfo.Ticker;
import com.jintoufs.service.baseinfo.MarketDepthService;
import com.jintoufs.service.baseinfo.MarketService;
import com.jintoufs.util.date.DateUtils;
import com.jintoufs.util.http.CoinwHttpsUtil;
import com.jintoufs.util.http.HttpRequest;
import com.jintoufs.util.http.HttpsClientUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;

/**
 * @author xiaoemo
 */
@Repository
public class MarketDepthServiceImpl implements MarketDepthService {
    @Resource
    private TickerDAO tickerDAO;
    @Resource
    private JhjRedis jhjRedis;

    @Override
    public void livecoin(Map<String, Object> site, List<Map<String, Object>> coins) {

    }

    @Override
    public void bithumb(Map<String, Object> site, List<Map<String, Object>> coins) {

    }

    @Override
    public void cexcom(Map<String, Object> site, List<Map<String, Object>> coins) {

    }

    @Override
    public Depth coinw(String symbol) {
        Depth depth = new Depth();
        String api = "https://www.coinw.me/appApi.html?action=depth&symbol=SYMBOL";
        api = api.replace("SYMBOL", symbol);
        String json = CoinwHttpsUtil.getStringByHttpsPost(api);
        json = CoinwHttpsUtil.getStringByHttpsPost(api);
        if ("error".equals(json)) {
            return depth;
        }
        JSONObject object = JSON.parseObject(json);
        if (object.get("code").toString().equals("200")) {
            String date = object.getString("time");
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(Long.parseLong(date));

            JSONObject data = object.getJSONObject("data");

            JSONArray buyArray = data.getJSONArray("bids");
            JSONArray sellArray = data.getJSONArray("asks");
            List buys = new ArrayList();
            List sells = new ArrayList();
            for (int i = 0; i < buyArray.size(); i++) {
                JSONObject job = buyArray.getJSONObject(i);
                Integer id = Integer.parseInt(job.get("id").toString());
                String amount = job.get("amount").toString();
                String price = job.get("price").toString();
                DepthUnit depthUnit = new DepthUnit();
                depthUnit.setId(id);
                depthUnit.setAmount(amount);
                depthUnit.setPrice(price);
                buys.add(depthUnit);
            }

            for (int i = 0; i < sellArray.size(); i++) {
                JSONObject job = sellArray.getJSONObject(i);
                Integer id = Integer.parseInt(job.get("id").toString());
                String amount = job.get("amount").toString();
                String price = job.get("price").toString();
                DepthUnit depthUnit = new DepthUnit();
                depthUnit.setId(id);
                depthUnit.setAmount(amount);
                depthUnit.setPrice(price);
                sells.add(depthUnit);
            }
            depth.setBuys(buys);
            depth.setSells(sells);
            depth.setDate(calendar.getTime());
        }
        return depth;
    }

    @Override
    public Depth liqui(Integer coinId, String coinName, String unitCode, Double unitPrice) {

        Depth depth = new Depth();
        String api = "https://api.liqui.io/api/3/depth/COIN_UNIT";
        api = api.replace("COIN", coinName);
        if ("USD".equalsIgnoreCase(unitCode)) {
            unitCode = "USDT";
        }
        api = api.replace("UNIT", unitCode);
        String json = "";
        api = api.toLowerCase();
        json = HttpsClientUtils.doGet(api.toLowerCase());

        JSONObject object = JSON.parseObject(json);

        JSONObject data = object.getJSONObject((coinName + "_" + unitCode).toLowerCase());

        JSONArray buyArray = data.getJSONArray("bids");
        JSONArray sellArray = data.getJSONArray("asks");
        List buys = new ArrayList();
        List sells = new ArrayList();
        for (int i = 0; i < buyArray.size() && i < 10; i++) {
            String price = buyArray.getJSONArray(i).get(0).toString();
            String amount = buyArray.getJSONArray(i).get(1).toString();
            DepthUnit depthUnit = new DepthUnit();
            depthUnit.setAmount(amount);
            depthUnit.setPrice(String.valueOf(Double.parseDouble(price) * unitPrice));
            buys.add(depthUnit);
        }

        for (int i = 0; i < sellArray.size() && i < 10; i++) {
            String price = sellArray.getJSONArray(i).get(0).toString();
            String amount = sellArray.getJSONArray(i).get(1).toString();
            DepthUnit depthUnit = new DepthUnit();
            depthUnit.setAmount(amount);
            depthUnit.setPrice(String.valueOf(Double.parseDouble(price) * unitPrice));
            sells.add(depthUnit);
        }
        depth.setBuys(buys);
        depth.setSells(sells);
        depth.setDate(new Date());
        return depth;
    }

    @Override
    public void bitfinex(Map<String, Object> site, List<Map<String, Object>> coins) {

    }

    @Override
    public void bitstamp(Map<String, Object> site, List<Map<String, Object>> coins) {

    }

    @Override
    public void poloniex(Map<String, Object> site, List<Map<String, Object>> coins) {

    }

    @Override
    public void bittrex(Map<String, Object> site, List<Map<String, Object>> coins) {

    }

    @Override
    public void okex(Map<String, Object> site, List<Map<String, Object>> coins) {

    }

    @Override
    public void zb(Map<String, Object> site, List<Map<String, Object>> coins) {

    }

    @Override
    public void wex(Map<String, Object> site, List<Map<String, Object>> coins) {

    }

    @Override
    public void allcoin(Map<String, Object> site, List<Map<String, Object>> coins) {

    }

    @Override
    public void kraken(Map<String, Object> site, List<Map<String, Object>> coins) {

    }

    @Override
    public void gate(Map<String, Object> site, List<Map<String, Object>> coins) {

    }

    @Override
    public void binance(Map<String, Object> site, List<Map<String, Object>> coins) {

    }

    @Override
    public void huobipro(Map<String, Object> site, List<Map<String, Object>> coins) {

    }
}
